(function (designWidth, maxWidth) {
    var doc = document,
        win = window;
    var docEl = doc.documentElement;
    var metaEl,
        metaElCon;
    var styleText,
        remStyle = document.createElement("style");
    var tid;

    function refreshRem() {
        // var width = parseInt(window.screen.width); // uc鏈塨ug
        var width = docEl.getBoundingClientRect().width;
        if (!maxWidth) {
            maxWidth = 540;
        };
        if (width > maxWidth) { // 娣樺疂鍋氭硶锛氶檺鍒跺湪540鐨勫睆骞曚笅锛岃繖鏍�100%灏辫窡10rem涓嶄竴鏍蜂簡
            width = maxWidth;
        }
        var rem = width * 100 / designWidth;
        // var rem = width / 10; // 濡傛灉瑕佸吋瀹箆w鐨勮瘽鍒嗘垚10浠� 娣樺疂鍋氭硶
        //docEl.style.fontSize = rem + "px"; //鏃х殑鍋氭硶锛屽湪uc娴忚鍣ㄤ笅闈細鏈夊垏鎹㈡í绔栧睆鏃跺畾涔変簡font-size鐨勬爣绛句笉璧蜂綔鐢ㄧ殑bug
        remStyle.innerHTML = 'html{font-size:' + rem + 'px;}';
    }

    // 璁剧疆 viewport 锛屾湁鐨勮瘽淇敼 娌℃湁鐨勮瘽璁剧疆
    metaEl = doc.querySelector('meta[name="viewport"]');
    // 20171219淇敼锛氬鍔� viewport-fit=cover 锛岀敤浜庨€傞厤iphoneX
    metaElCon = "width=device-width,initial-scale=1,maximum-scale=1.0,user-scalable=no,viewport-fit=cover";
    if (metaEl) {
        metaEl.setAttribute("content", metaElCon);
    } else {
        metaEl = doc.createElement("meta");
        metaEl.setAttribute("name", "viewport");
        metaEl.setAttribute("content", metaElCon);
        if (docEl.firstElementChild) {
            docEl.firstElementChild.appendChild(metaEl);
        } else {
            var wrap = doc.createElement("div");
            wrap.appendChild(metaEl);
            doc.write(wrap.innerHTML);
            wrap = null;
        }
    }

    //瑕佺瓑 wiewport 璁剧疆濂藉悗鎵嶈兘鎵ц refreshRem锛屼笉鐒� refreshRem 浼氭墽琛�2娆★紱
    refreshRem();

    if (docEl.firstElementChild) {
        docEl.firstElementChild.appendChild(remStyle);
    } else {
        var wrap = doc.createElement("div");
        wrap.appendChild(remStyle);
        doc.write(wrap.innerHTML);
        wrap = null;
    }

    win.addEventListener("resize", function () {
        clearTimeout(tid); //闃叉鎵ц涓ゆ
        tid = setTimeout(refreshRem, 300);
    }, false);

    win.addEventListener("pageshow", function (e) {
        if (e.persisted) { // 娴忚鍣ㄥ悗閫€鐨勬椂鍊欓噸鏂拌绠�
            clearTimeout(tid);
            tid = setTimeout(refreshRem, 300);
        }
    }, false);

    if (doc.readyState === "complete") {
        doc.body.style.fontSize = "16px";
    } else {
        doc.addEventListener("DOMContentLoaded", function (e) {
            doc.body.style.fontSize = "16px";
        }, false);
    }
})(750, 750);